package entidadesBD;

import java.io.Serializable;
import java.sql.Date;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import entidadesVO.*;

@Entity
@Table(name = "sol_dist")
public class SolDist implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Date fechaSolDist;
	private CentroDistribucion centroDistribucion;
	private Set<ArticulosSolDist> articulosSolDist = new HashSet<ArticulosSolDist>();
	
	private int codigoSolDist;

	public SolDist(Date fechaSolDist, CentroDistribucion centroDistribucion) {
		this.fechaSolDist = fechaSolDist;
		this.centroDistribucion = centroDistribucion;
	}
	/*
	 * Constructor Vacio
	 */
	public SolDist(){
		
	}
	
	@OneToOne
	@JoinColumn(name = "centro_distribucion")
	public CentroDistribucion getCentroDistribucion() {
		return centroDistribucion;
	}
	public void setCentroDistribucion(CentroDistribucion centroDistribucion) {
		this.centroDistribucion = centroDistribucion;
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getCodigoSolDist() {
		return codigoSolDist;
	}
	public void setCodigoSolDist(int codigoSolDist) {
		this.codigoSolDist = codigoSolDist;
	}
	
	@Column
	public Date getFechaSolDist() {
		return fechaSolDist;
	}
	public void setFechaSolDist(Date fechaSolDist) {
		this.fechaSolDist = fechaSolDist;
	}
	
	@OneToMany(mappedBy = "solDist", fetch = FetchType.EAGER, cascade={CascadeType.ALL})
	public Collection<ArticulosSolDist> getArticulosSolDist() {
		return articulosSolDist;
	}
	public void setArticulosSolDist(Set<ArticulosSolDist> articulosSolDist) {
		this.articulosSolDist = articulosSolDist;
	}
	public void addArticulosSolDist(ArticulosSolDist articulosSolDist){
		this.articulosSolDist.add(articulosSolDist);
	}
	
	@Transient
	public SolDistVO getSolDistVO(){
		
		CentroDistribucionVO centroDistribucionVO = new CentroDistribucionVO();
		centroDistribucionVO = centroDistribucion.getCentroDistribucionVO();
	
		SolDistVO solDistVO = new SolDistVO(fechaSolDist, centroDistribucionVO);
		solDistVO.setCodigoSolDist(codigoSolDist);
		
		Object[] articulosSolDistArray = articulosSolDist.toArray();
		
		for (int i = 0; i < articulosSolDistArray.length; i++){
			ArticulosSolDistVO aSDVO = new ArticulosSolDistVO();
			ArticulosSolDist aSD = (ArticulosSolDist) articulosSolDistArray[i];
			aSDVO = aSD.getArticulosSolDistVO();
			solDistVO.addArticulosSolDist(aSDVO);
		}
	
		return solDistVO;
	}
	
	public void setSolDist(SolDistVO solDistVO){
		
		this.fechaSolDist = solDistVO.getFechaSolDist();
		
	}
}
